    //
//  InternetViewController.m
//  CommonFramework
//
//  Created by Bruno Bilescky on 22/07/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "InternetViewController.h"


@interface InternetViewController (Private)

-(void)startLoading;
-(void)beforeLoadContentFromInternet;
-(void)finishLoadingContentFromInternet;
-(void)showLoadingView;
-(void)hideLoadingView;
-(void)finishHiding;

@end

@implementation InternetViewController

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)loadRemoteContent {
	[self performSelectorInBackground:@selector(startLoading) withObject:nil];
}

-(void)startLoading {
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
	[self performSelectorOnMainThread:@selector(beforeLoadContentFromInternet) withObject:nil waitUntilDone:YES];
	[self loadContentFromInternet];
	[self performSelectorOnMainThread:@selector(finishLoadingContentFromInternet) withObject:nil waitUntilDone:YES];
	[pool release];
}

-(void)beforeLoadContentFromInternet {
	[self showLoadingView];
	[self willLoadContentFromInternet];
}

-(void)finishLoadingContentFromInternet {
	[self didLoadContentFromInternet];
	[self hideLoadingView];
}

-(UIView*)newLoadingView {
	UIView* view = [[UIView alloc] initWithFrame:CGRectMake(220, 370, 85, 25)];
	view.backgroundColor = [UIColor darkGrayColor];
	
	UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(25, 3, 55, 20)];
	lbl.text = @"Loading";
	lbl.backgroundColor = [UIColor clearColor];
	lbl.textAlignment = UITextAlignmentRight;
	lbl.textColor = [UIColor whiteColor];
	lbl.font = [UIFont boldSystemFontOfSize:13];
	[view addSubview:lbl];
	[lbl release];
	UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(3, 0, 25, 25)];
	imgView.contentMode = UIViewContentModeScaleAspectFit;
	imgView.image = [UIImage imageNamed:@"connected.png"];
	[view addSubview:imgView];
	[imgView release];
	CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    fullRotation.fromValue = [NSNumber numberWithFloat:0];
    fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
    fullRotation.duration = 1;
    fullRotation.repeatCount = 1e100f;
    [imgView.layer addAnimation:fullRotation forKey:@"360"];
	return view;
}

-(CGRect)defineFrameForLoadingView {
	//for (UIView* view in self.view.subviews
	return CGRectMake(220, 370, 85, 25);
}

-(void)showLoadingView {
	loadingView = [self newLoadingView];
	loadingView.alpha = 0;
	if (loadingView){
		[self.view addSubview:loadingView];
		[UIView beginAnimations:@"showLoading" context:nil];
		[UIView setAnimationDuration:1.0];
		loadingView.alpha = 1;
		[UIView commitAnimations];
	}
}

-(void)hideLoadingView {
	if (loadingView){
		[UIView beginAnimations:@"showLoading" context:nil];
		[UIView setAnimationDuration:0.5];
		[UIView setAnimationDidStopSelector:@selector(finishHiding)];
		loadingView.alpha = 0;
		[UIView commitAnimations];
	}
}

-(void)finishHiding {
	[loadingView removeFromSuperview];
	[loadingView release];
	loadingView = nil;
}

-(void)willLoadContentFromInternet { }

-(void)loadContentFromInternet { }

-(void)didLoadContentFromInternet { }


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
